package cn.com.smartdevices.bracelet.chart;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextPaint;

import com.goodving.guli.R;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.utils.C0893f;
import com.goodving.guli.view.C4220a;

import java.util.List;

import cn.com.smartdevices.bracelet.chart.base.BarChart;
import cn.com.smartdevices.bracelet.chart.util.AnimUtil;
import cn.com.smartdevices.bracelet.chart.util.C0804t;

/* renamed from: cn.com.smartdevices.bracelet.chart.a */
public class DynamicDetailChart extends BarChart {
    private static final String f3933a = "Chart.DynamicDetailChart";
    private static final int f3934b = 600;
    private static final float f3935x = 196.6f;
    private static final float f3936y = 160.0f;
    private C0764f f3937A;
    private int f3938B;
    private int f3939C;
    private int f3940D;
    private int f3941E;
    private int f3942F;
    private C0763e f3943z;

    public DynamicDetailChart(Context context) {
        super(context);
        this.mXAxis = new C0750b(this, context);
        this.mXAxis.stroke = (int) (C4220a.bf * this.mDensity);
        this.mXAxis.setPadding((float) ((int) (((double) this.mDensity) * 2.5d)), 0.0f, (float) ((int) (((double) this.mDensity) * 2.5d)), 0.0f);
        this.f3943z = new C0763e(this, context);
        this.f3943z.setPadding((float) ((int) (this.mDensity * 8.0f)), (float) ((int) (130.0f * this.mDensity)), (float) ((int) (this.mDensity * 8.0f)), 0.0f);
        this.f3943z.setItemPadding(1.0f);
        this.f3937A = new C0764f(this, context);
        this.f3937A.setPadding((float) ((int) (((double) this.mDensity) * 2.5d)), (float) ((int) (95.0f * this.mDensity)), (float) ((int) (((double) this.mDensity) * 2.5d)), 0.0f);
        this.f3937A.setMaxItemValue(f3934b);
        this.f3937A.setItemPadding(0.5f * this.mDensity);
    }

    static /* synthetic */ int m8579c(DynamicDetailChart c0745a, int i) {
        int i2 = c0745a.f3940D - i;
        c0745a.f3940D = i2;
        return i2;
    }

    static /* synthetic */ int m8581d(DynamicDetailChart c0745a, int i) {
        int i2 = c0745a.f3941E + i;
        c0745a.f3941E = i2;
        return i2;
    }

    class C0761c extends BarChart {
        final /* synthetic */ DynamicDetailChart f3987a;

        public C0761c(DynamicDetailChart c0745a, Context context) {
            super(context);
            this.f3987a = c0745a;

        }

        protected void draw(Canvas canvas, RectF rectF, float f) { // m8677a
            synchronized (this.mItems) {
                int i = 0;
                for (BarItem c0726c : this.mItems) {
                    i = c0726c.f3886e ? i + 1 : i;
                }
                int i2 = 0;
                for (BarItem c0726c2 : this.mItems) {
                    if (c0726c2.f3886e) {
                        float f2 = f / (1.0f - (((float) i2) * (0.6f / ((float) i))));
                        if (f2 > 1.0f) {
                            f2 = 1.0f;
                        }
                        c0726c2.draw(canvas, f2);
                        i2++;
                    }
                }
            }
        }
    }

    class C0764f extends C0761c {
        final /* synthetic */ DynamicDetailChart f3989b;
        private float f3990x;
        private int f3991y;

        public C0764f(DynamicDetailChart c0745a, Context context) {
            super(c0745a, context);
            this.f3989b = c0745a;

            this.f3991y = 10;
        }

        protected float itemWidth (RectF rectF, BarItem c0726c) { // m8681a
            return (((rectF.width() - this.mPaddingLeft) - this.mPaddingRight) / ((float) ((this.f3989b.f3941E - this.f3989b.f3940D) + 1))) - (this.mItemPadding * 2.0f);
        }

        protected void onRectChanged(RectF rectF) { // m8682a
            super.onRectChanged(rectF);
            if (rectF != null) {
                this.f3990x = C0804t.m8822a(this.mMaxItemValue, (rectF.height() - this.mPaddingTop) - this.mPaddingBottom, this.f3991y);
            }
        }

        protected void onItemsChanged(List<BarItem> list) {
            super.onItemsChanged(list);
            this.mMaxItemValue = C0804t.m8827a((List) list, 0, (int) AnimUtil.f4062b);
            if (this.mRect != null) {
                this.f3990x = C0804t.m8822a(this.mMaxItemValue, (this.mRect.height() - this.mPaddingTop) - this.mPaddingBottom, this.f3991y);
            }
            this.f3989b.f3940D = Integer.MAX_VALUE;
            this.f3989b.f3941E = 0;
            for (BarItem c0726c : list) {
                GuliLog.tracePrintInfo("Chart.DynamicDetailChart", "Step Item : " + c0726c);
                if (c0726c.f3884c < this.f3989b.f3940D) {
                    this.f3989b.f3940D = c0726c.f3884c;
                }
                if (c0726c.f3884c > this.f3989b.f3941E) {
                    this.f3989b.f3941E = c0726c.f3884c;
                }
            }
            if (list.size() == 0) {
                this.f3989b.f3940D = 0;
                this.f3989b.f3941E = 47;
            } else {
                if (this.f3989b.f3940D % 6 != 0) {
                    DynamicDetailChart.m8579c(this.f3989b, this.f3989b.f3940D % 6);
                }
                if ((this.f3989b.f3941E + 1) % 6 != 0) {
                    DynamicDetailChart.m8581d(this.f3989b, 6 - ((this.f3989b.f3941E + 1) % 6));
                }
                if ((this.f3989b.f3941E - this.f3989b.f3940D) + 1 < 48) {
                    this.f3989b.f3940D = (this.f3989b.f3941E - 48) + 1;
                }
            }
            this.f3989b.f3938B = this.f3989b.f3940D * 60;
            this.f3989b.f3939C = this.f3989b.f3941E * 60;
            ((C0750b) this.f3989b.mXAxis).notifyChanged();
        }

        protected float itemHeight (RectF rectF, BarItem c0726c) { // m8683b
            return c0726c.f3883b >= this.mMaxItemValue ? (rectF.height() - this.mPaddingTop) - this.mPaddingBottom : C0804t.m8823a(this.mMaxItemValue, c0726c.f3883b, this.f3990x, this.f3991y);
        }

        protected float itemOffsetX (RectF rectF, BarItem c0726c) { // m8684c
            return ((C0750b) this.f3989b.mXAxis).m8632c() + (this.mPaddingLeft + ((((rectF.width() - this.mPaddingLeft) - this.mPaddingRight) / ((float) ((this.f3989b.f3941E - this.f3989b.f3940D) + 1))) * ((float) (c0726c.f3884c - this.f3989b.f3940D))));
        }
    }

    class C0763e extends C0761c {
        final /* synthetic */ DynamicDetailChart f3988b;

        public C0763e(DynamicDetailChart c0745a, Context context) {
            super(c0745a, context);
            this.f3988b = c0745a;

        }

        protected float itemWidth (RectF rectF, BarItem c0726c) {  // m8678a
            if (c0726c.f3884c > this.f3988b.f3939C || c0726c.f3884c < this.f3988b.f3938B || this.f3988b.f3938B == this.f3988b.f3939C) {
                return 0.0f;
            }
            float width = ((((rectF.width() - this.mPaddingLeft) - this.mPaddingRight) / ((float) ((this.f3988b.f3939C - this.f3988b.f3938B) + 1))) * ((float) c0726c.f3885d)) - (this.mItemPadding * 2.0f);
            return width < this.mDensity * 1.0f ? this.mDensity * 1.0f : width;
        }

        protected void onItemsChanged(List<BarItem> list) {
            if (this.f3988b.f3938B != this.f3988b.f3939C) {
                super.onItemsChanged(list);
            }
        }

        protected float itemHeight (RectF rectF, BarItem c0726c) {  // m8679b
            return c0726c.f3883b == 3 ? 196.6f * this.mDensity : (c0726c.f3883b == 2 || c0726c.f3883b == 1 || c0726c.f3883b == 4) ? 160.0f * this.mDensity : 0.0f;
        }

        protected float itemOffsetX (RectF rectF, BarItem c0726c) { // m8680c
            if (c0726c.f3884c > this.f3988b.f3939C || c0726c.f3884c < this.f3988b.f3938B || this.f3988b.f3938B == this.f3988b.f3939C) {
                return 0.0f;
            }
            return ((C0750b) this.f3988b.mXAxis).m8632c() + (this.mPaddingLeft + ((((rectF.width() - this.mPaddingLeft) - this.mPaddingRight) / ((float) ((this.f3988b.f3939C - this.f3988b.f3938B) + 1))) * ((float) (c0726c.f3884c - this.f3988b.f3938B))));
        }
    }

    class C0750b extends XAxis {
        final /* synthetic */ DynamicDetailChart f3950a;
        private int f3951b;
        private float f3952c;
        private Paint f3953d;
        private Paint f3954e;
        private Bitmap f3955i;
        private Bitmap f3956x;

        public C0750b(DynamicDetailChart c0745a, Context context) {
            super(context);
            this.f3950a = c0745a;

            this.f3951b = 0;
            this.value = 24;
            this.offset = this.f3951b;
            this.f3953d = new TextPaint(1);
            this.f3953d.setColor(1711276032);
            this.f3953d.setTextSize(9.0f * this.mDensity);
            this.f3954e = new Paint();
            this.f3954e.setColor(0xfff2f2f2);
        }

        private void m8624a(Canvas canvas, RectF rectF) {
            if (this.f3950a.f3938B != this.f3950a.f3939C) {
                Rect rect = new Rect();
                float f = (8.0f * this.mDensity) + rectF.left;
                float centerY = rectF.centerY();
                C0804t.m8831a(canvas, f, centerY, this.mDensityScale, false, true, this.f3955i, null);
                String a = C0893f.m9085a(this.f3950a.f3938B);
                this.f3953d.getTextBounds(a, 0, a.length(), rect);
                rect.offset((int) (f + (15.0f * this.mDensity)), (int) (((double) (centerY + ((float) (rect.height() / 2)))) - (0.66d * ((double) this.mDensity))));
                C0804t.m8833a(canvas, new RectF(rect), a, this.f3953d);
                f = (rectF.right - (8.0f * this.mDensity)) - (((float) this.f3956x.getWidth()) * this.mDensityScale);
                centerY = rectF.centerY();
                C0804t.m8831a(canvas, f, centerY, this.mDensityScale, false, true, this.f3956x, null);
                a = C0893f.m9085a(this.f3950a.f3939C);
                this.f3953d.getTextBounds(a, 0, a.length(), rect);
                rect.offset((int) ((f - ((float) rect.width())) - (6.0f * this.mDensity)), (int) (((double) (centerY + ((float) (rect.height() / 2)))) - (0.66d * ((double) this.mDensity))));
                C0804t.m8833a(canvas, new RectF(rect), a, this.f3953d);
            }
        }

        private void m8625b(Canvas canvas, RectF rectF) {
            if (this.f3950a.f3940D != this.f3950a.f3941E) {
                Rect rect = new Rect();
                int i = ((this.f3950a.f3941E - this.f3950a.f3940D) + 1) / 6 >= 20 ? 1 : 0;
                int i2 = Integer.MIN_VALUE;
                for (int i3 = 0; i3 < (this.f3950a.f3941E - this.f3950a.f3940D) + 1; i3++) {
                    int a = (this.f3950a.f3940D + i3) / 6;
                    if (a < 0) {
                        a += 24;
                    }
                    if (a % 2 == 0 && a != i2) { //TBD
                        float f = (this.mScroll / this.f3952c) % 1.0f == 0.0f ? 0.0f : this.mScroll % this.f3952c;
                        String b = i == 0 ? C0893f.m9089b(a * 60) : String.valueOf(a);
                        this.f3953d.getTextBounds(b, 0, b.length(), rect);
                        canvas.drawText(b, f + ((((((float) i3) * this.f3952c) + (((this.f3952c * 6.0f) - ((float) rect.width())) / 2.0f)) + rectF.left) + this.mPaddingLeft), (((float) (this.stroke + rect.height())) / 2.0f) + rectF.top, this.f3953d);
                        i2 = a;
                    }
                }
            }
        }

        private void m8626j() {
            if (this.f3955i == null) {
                this.f3955i = ((BitmapDrawable) this.mContext.getResources().getDrawable(R.drawable.ic_dynamic_detail_sleep_start)).getBitmap();
            }
            if (this.f3956x == null) {
                this.f3956x = ((BitmapDrawable) this.mContext.getResources().getDrawable(R.drawable.ic_dynamic_detail_sleep_end)).getBitmap();
            }
        }

        public void m8627a() {
            this.mScroll = ((float) (this.f3951b - this.offset)) * this.f3952c;
            this.f3950a.mScroll = this.mScroll;
        }

        protected void doScroll(float f) { // m8628a
            this.offset = (-((int) (this.mScroll / this.f3952c))) + this.f3951b;
        }

        protected void draw(Canvas canvas, RectF rectF, float f) { // m8629a
            canvas.drawRect(rectF, this.f3954e);
            if (this.f3950a.f3942F == 16) {
                m8626j();
                m8624a(canvas, rectF);
            } else if (this.f3950a.f3942F == 1) {
               m8625b(canvas, rectF);
            }
        }

        protected void onRectChanged (RectF rectF) { // m8630a
            m8627a();
        }

        public float m8631b() {
            return this.f3952c * ((float) this.value);
        }

        public float m8632c() {
            return this.mScroll - (((float) this.f3951b) * this.f3952c);
        }

        public float m8633d() {
            return ((float) this.f3951b) * this.f3952c;
        }

        public void notifyChanged() { // m8634e
            if (this.mRect != null && this.f3950a.f3940D != this.f3950a.f3941E) {
                this.f3952c = ((this.mRect.width() - this.mPaddingLeft) - this.mPaddingRight) / ((float) ((this.f3950a.f3941E - this.f3950a.f3940D) + 1));
            }
        }

        public float m8635f() {
            int i = (-this.offset) % this.value;
            if (m8636g()) {
                i = ((-this.offset) % this.value) + this.value;
            }
            return (((float) i) * this.f3952c) + (this.mScroll % this.f3952c);
        }

        public boolean m8636g() {
            return this.mScroll - (((float) this.f3951b) * this.f3952c) < 0.0f && (this.mScroll - (((float) this.f3951b) * this.f3952c)) % m8631b() < 0.0f;
        }

        public int m8637h() {
            return this.offset / this.value;
        }

        public float m8638i() {
            return m8636g() ? (((float) (-m8637h())) * m8631b()) + (this.f3952c * ((float) this.f3951b)) : ((float) (-m8637h())) * m8631b();
        }
    }

    public void clearStepData () { // m8588a
        this.f3937A.clearItems();
        this.f3938B = 0;
        this.f3939C = 0;
        this.f3940D = 0;
        this.f3941E = 0;
    }

    protected void doScroll (float f) { // m8589a
        GuliLog.tracePrintWarn(f3933a, "Scroll :" + f);
        if (this.mLoadCallback != null) {
            C0750b c0750b = (C0750b) this.mXAxis;
            if (this.mScroll > 0.0f) {
                this.mScroll = 0.0f;
            }
            if (this.mScroll < c0750b.m8633d()) {
                this.mScroll = c0750b.m8633d();
            }
            GuliLog.tracePrintWarn(f3933a, "ScrollTo : " + this.mScroll);
            c0750b.scrollTo(this.mScroll);
        }
    }

    public void setMode (int i) { // m8590a
        this.f3942F = i;
    }

    public void setStartEndTimeIndex (int i, int i2) { // m8591a
        GuliLog.tracePrintInfo(f3933a, "Sleep Time : " + i + " , " + i2);
        this.f3938B = i;
        this.f3939C = i2;
        this.f3940D = this.f3938B / 60;
        this.f3941E = this.f3939C / 60;
        if (this.f3938B == this.f3939C) {
            justifyStartEndTimeIndex(8);
        }
    }

    public void draw (Canvas canvas, float f) { // m8592a
        if (!getParentView().m8404k()) {
            this.f3943z.draw(canvas, f);
            this.f3937A.draw(canvas, f);
        }
        super.draw(canvas, f);
    }

    protected void onRectChanged (RectF rectF) { //m8593a
        super.onRectChanged(rectF);
        RectF m = this.mXAxis.getRect();
        RectF rectF2 = new RectF(rectF);
        rectF2.bottom = m.top;
        this.f3937A.setRect(rectF2);
        rectF2 = new RectF(rectF);
        rectF2.bottom = m.top;
        this.f3943z.setRect(rectF2);
    }

    public void addStepData (List<C0762d> list) { // m8594a
        this.f3937A.addItems(list);
    }

    public void clearSleepData () {// m8595b
        this.f3943z.clearItems();
        this.f3938B = 0;
        this.f3939C = 0;
        this.f3940D = 0;
        this.f3941E = 0;
    }

    public void setOffset (int i) { // m8596b
        C0750b c0750b = (C0750b) this.mXAxis;
        c0750b.f3951b = i;
        c0750b.offset = 0;
        c0750b.m8627a();
    }

    public void addSleepData (List<C0762d> list) { // m8597b
        this.f3943z.addItems(list);
        this.f3943z.sortItems();
    }

    public BarChart getSleepChart () { //m8598c
        return this.f3943z;
    }

    public void justifyStartEndTimeIndex (int i) { // m8599c
        GuliLog.tracePrintInfo(f3933a, "Before, Start : " + this.f3938B + " End : " + this.f3939C + " , StartHour : " + this.f3940D + " EndHour : " + this.f3941E);
        if (this.f3941E - this.f3940D < i - 1) {
            this.f3941E = this.f3940D + (i - 1);
            if (this.f3941E > 23) {
                this.f3941E = 23;
                this.f3940D = this.f3941E - (i - 1);
            }
            this.f3938B = this.f3940D * 60;
            this.f3939C = this.f3941E * 60;
        }
        GuliLog.tracePrintInfo(f3933a, "After, Start : " + this.f3938B + " End : " + this.f3939C + " , StartHour : " + this.f3940D + " EndHour : " + this.f3941E);
        ((C0750b) this.mXAxis).notifyChanged();
    }

    public void fillSleepData (List<BarItem> list) { //m8600c
        this.f3943z.fillItems(list);
    }

    public BarChart getStepChart () { // m8601d
        return this.f3937A;
    }

    public void fillStepData (List<C0762d> list) { // m8602d
        this.f3937A.fillItems(list);
    }

    public void notifyChanged () { // m8603e
        this.f3943z.notifyChanged();
        this.f3937A.notifyChanged();
    }

    public int getStartTimeIndex () { // m8604f
        return this.f3938B;
    }

    public int getEndTimeIndex () { // m8605g
        return this.f3939C;
    }

    public float getEdgeLength () { // m8606h
        return ((C0750b) this.mXAxis).m8633d();
    }
}
